/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.tor.util.renderer;

import de.tor.util.EditorRenderer;
import de.tor.util.types.Screen;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author Charon
 */
public class EditorRendererRenderer extends Thread {

    private BufferedImage mBuffer = null;
    private List<Screen> mScreens = null;
    private Point mVirtualPosition = null;

    public EditorRendererRenderer() {
        mScreens = new LinkedList<Screen>();
        mVirtualPosition = new Point(0, 0);
    }

    public void addScreen(File pScreen) throws Exception {
        Screen toAdd = new Screen(pScreen.getPath());
        if (!mScreens.isEmpty()) {
            int w = mScreens.get(0).getImage().getWidth();
            int h = mScreens.get(0).getImage().getHeight();
            if (w != toAdd.getImage().getWidth() || h != toAdd.getImage().getHeight()) {
                throw new Exception("Size not equal");
            }
        }
        mScreens.add(toAdd);
    }

    public void removeScreen(int pIdx) {
        mScreens.remove(pIdx);
    }

    public void moveScreenUp(int pIdx) {
        if (pIdx - 1 < 0) {
            //first screen
            return;
        }
        Screen s = mScreens.remove(pIdx);
        mScreens.add(pIdx - 1, s);
    }

    public void moveScreenDown(int pIdx) {
        if (pIdx + 1 > mScreens.size() - 1) {
            //first screen
            return;
        }
        Screen s = mScreens.remove(pIdx);
        mScreens.add(pIdx + 1, s);
    }

    public List<Screen> getScreens() {
        return mScreens;
    }

    public Dimension getVirtualSize() {
        Screen s = mScreens.get(0);
        return new Dimension(s.getImage().getWidth(), mScreens.size() * s.getImage().getHeight());
    }

    public void moveVirtualPosition(int pDx, int pDy) {
        /* if ((mVirtualPosition.x + pDx >= 0) &&
        (mVirtualPosition.x + EditorRenderer.getSingleton().getWidth() + pDy <= getVirtualSize().getWidth()) &&
        (mVirtualPosition.y + pDy >= 0) &&
        (mVirtualPosition.y + EditorRenderer.getSingleton().getHeight() + pDy <= getVirtualSize().getHeight())) {

         */

        mVirtualPosition.move(mVirtualPosition.x + pDx, mVirtualPosition.y + pDy);
    // }
    }

    public void run() {
        while (true) {
            try {
                initBuffer();
                Graphics2D g2d = (Graphics2D) mBuffer.getGraphics();
                g2d.setColor(Color.GREEN);
                g2d.fillRect(0, 0, mBuffer.getWidth(), mBuffer.getHeight());
                Screen s = mScreens.get(0);
                int h = EditorRenderer.getSingleton().getHeight();
                int w = EditorRenderer.getSingleton().getWidth();
                int screenCount = h / s.getImage().getHeight() + 2;
                int screenIdx = mVirtualPosition.y / s.getImage().getHeight();
                s = mScreens.get(screenIdx);
                int dx = mVirtualPosition.x;
                int dy = mVirtualPosition.y - screenIdx * s.getImage().getHeight();
                // System.out.println("Delta " + dx + "," + dy);
                g2d.drawImage(s.getImage(), -dx, -dy, null);
                for (int i = 1; i < screenCount; i++) {
                    if (screenIdx + i < mScreens.size()) {
                        g2d.drawImage(mScreens.get(screenIdx + i).getImage(), -dx, -dy + i * s.getImage().getHeight(), null);
                    }
                }
                EditorRenderer.getSingleton().updateBuffer(mBuffer);
                g2d.dispose();
                try {
                    Thread.sleep(100);
                } catch (Exception e) {
                }
            } catch (IndexOutOfBoundsException ioe) {
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void initBuffer() {
        try {
            if (mBuffer == null || mBuffer.getWidth() != EditorRenderer.getSingleton().getWidth() || mBuffer.getHeight() != EditorRenderer.getSingleton().getHeight()) {
                mBuffer = new BufferedImage(EditorRenderer.getSingleton().getWidth(), EditorRenderer.getSingleton().getHeight(), BufferedImage.TYPE_INT_ARGB);
            }
        } catch (Exception e) {
        }
    }
}
